using AutoMapper;
using B.S.XZYData.Api.Read.Application.Command.Product;
using B.S.XZYData.Api.Read.Dto.Product;
using B.S.XZYData.Domain.LJC;
using B.S.XZYData.ErrorCode;
using B.S.XZYData.Infrastructure.Interface;
using MediatR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace B.S.XZYData.Api.Read.Application.Handler.Product
{
    /// <summary>
    /// 获取产品分页列表处理器
    /// </summary>
    public class GetProductPageHandler : IRequestHandler<GetProductPageCommand, ApiPageing<ProductDto>>
    {
        private readonly IBaseRepository<ProductModel> _productRepo;
        private readonly IMapper _mapper;

        public GetProductPageHandler(
            IBaseRepository<ProductModel> productRepo,
            IMapper mapper)
        {
            _productRepo = productRepo;
            _mapper = mapper;
        }

        public async Task<ApiPageing<ProductDto>> Handle(GetProductPageCommand request, CancellationToken cancellationToken)
        {
            var res = new ApiPageing<ProductDto>();

            try
            {
                // 构建查询条件
                var query = _productRepo.GetAll();

                // 产品编号模糊查询
                if (!string.IsNullOrWhiteSpace(request.Code))
                {
                    query = query.Where(x => x.Code.Contains(request.Code));
                }

                // 产品名称模糊查询
                if (!string.IsNullOrWhiteSpace(request.Name))
                {
                    query = query.Where(x => x.Name.Contains(request.Name));
                }

                // 规格模糊查询
                if (!string.IsNullOrWhiteSpace(request.Spec))
                {
                    query = query.Where(x => x.Spec != null && x.Spec.Contains(request.Spec));
                }

                // 单位精确查询
                if (!string.IsNullOrWhiteSpace(request.Unit))
                {
                    query = query.Where(x => x.Unit == request.Unit);
                }

                // 生产人员模糊查询
                if (!string.IsNullOrWhiteSpace(request.Producer))
                {
                    query = query.Where(x => x.Producer != null && x.Producer.Contains(request.Producer));
                }

                // 排序（按产品编号）
                query = query.OrderBy(x => x.Code);

                // 获取总数
                var totalCount = query.Count();

                // 分页
                var products = query
                    .Skip((request.Index - 1) * request.Size)
                    .Take(request.Size)
                    .ToList();

                // 映射到 DTO
                var productDtos = _mapper.Map<List<ProductDto>>(products);

                res.Code = ApiEnum.Success;
                res.Msg = "查询成功";
                res.PageData = productDtos;
                res.TotaCount = totalCount;
            }
            catch (Exception ex)
            {
                res.Code = ApiEnum.Error;
                res.Msg = $"查询失败：{ex.Message}";
                res.PageData = new List<ProductDto>();
                res.TotaCount = 0;
            }

            return res;
        }
    }
} 